package pl.lodz.p.model.utils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import pl.lodz.p.model.User;
import pl.lodz.p.services.UserService;
import pl.lodz.p.services.UserServiceImpl;

public class UserConverter implements Converter {
 
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            UserService service = new UserServiceImpl();
            return service.findByLogin(value);
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            return ((User) object).getLogin();
        }
        else {
            return null;
        }
    }   
} 